package site.beanyon.now.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.beanyon.now.constant.ResponseEnum;
import site.beanyon.now.model.dto.HttpResponse;
import site.beanyon.now.model.dto.TimePlanDTO;
import site.beanyon.now.model.po.TimePlanPO;
import site.beanyon.now.service.TimePlanService;
import site.beanyon.now.service.UserService;
import site.beanyon.now.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 时间计划控制器
 */
@RestController
@RequestMapping("/time-plan")
public class TimePlanController {
    @Autowired
    private TimePlanService timePlanService;

    @Autowired
    private UserService userService;

    /**
     * 获取时间计划列表
     *
     * @return 时间计划列表
     */
    @GetMapping("/list")
    public HttpResponse list() {
        List<TimePlanDTO> timePlanDTOList = this.timePlanService.listTimePlan();
        return new HttpResponse(ResponseEnum.SUCCESS, timePlanDTOList);
    }

    /**
     * 删除时间计划
     *
     * @param params 参数Map
     * @return 删除结果
     */
    @PostMapping("/delete")
    public HttpResponse delete(@RequestBody Map<String, String> params) {
        // 获取要删除的时间计划的id并判空
        String timePlanId = params.get("id");
        if (StringUtils.isEmpty(timePlanId)) {
            return new HttpResponse(ResponseEnum.TIME_PLAN_ID_IS_NULL, null);
        }

        // 删除时间计划
        boolean success = this.timePlanService.deleteTimePlan(Integer.valueOf(timePlanId));

        // 删除成功
        if (success) {
            return new HttpResponse(ResponseEnum.SUCCESS_DELETE_TIME_PLAN, null);
        }

        // 删除失败
        return new HttpResponse(ResponseEnum.FAILED_DELETE_TIME_PLAN, null);
    }

    /**
     * 创建时间计划
     *
     * @param params 参数Map
     * @return 创建结果
     */
    @PostMapping("/create")
    public HttpResponse create(@RequestBody Map<String, String> params) {
        // 获取活动名称并判空
        String activity = params.get("activity");
        if (StringUtils.isEmpty(activity)) {
            return new HttpResponse(ResponseEnum.ACTIVITY_IS_NULL, null);
        }

        // 创建时间计划
        TimePlanPO timePlanPO = new TimePlanPO();
        timePlanPO.setActivity(activity);
        timePlanPO.setUserId(this.userService.getCurrentUser().getId());
        this.timePlanService.createTimePlan(timePlanPO);

        // 创建失败
        if (timePlanPO.getId() == null) {
            return new HttpResponse(ResponseEnum.FAILED_CREATE_TIME_PLAN, null);
        }

        // 创建成功，返回时间计划在数据库中的id
        return new HttpResponse(ResponseEnum.SUCCESS_CREATE_TIME_PLAN, timePlanPO.getId());
    }

    /**
     * 更新时间计划的状态
     *
     * @param params 参数Map
     * @return 更新结果
     */
    @PostMapping("/update/status")
    public HttpResponse updateStatus(@RequestBody Map<String, String> params) {
        // 获取要更新的时间计划的id并判空
        String timePlanId = params.get("id");

        if (StringUtils.isEmpty(timePlanId)) {
            return new HttpResponse(ResponseEnum.TIME_PLAN_ID_IS_NULL, null);
        }

        // 更新时间计划状态
        boolean success = this.timePlanService.updateTimePlanStatus(Integer.valueOf(timePlanId));

        // 更新成功
        if (success) {
            return new HttpResponse(ResponseEnum.SUCCESS_UPDATE_TIME_PLAN, null);
        }

        // 更新失败
        return new HttpResponse(ResponseEnum.FAILED_UPDATE_TIME_PLAN, null);
    }
}
